package com.stageadminserver.adminserver.controller;

import com.stageadminserver.adminserver.service.CategoryService;
import com.stagecommon.common.pojo.Category;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.vo.CategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: yxl
 * @since: 2021-11-22
 **/
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有二级分类
     * @return
     */
    //二级分类
    @GetMapping("/listSecondLevelCategories")
    public RespBean listSecondLevelCategories(){
        return categoryService.listSecondCategories();
    }

    /**
     * 分页获取所有分类
     * @param currentPage
     * @param size
     * @param category
     * @return
     */
    @GetMapping("/getAllCategory")
    public RespBean getAllCategory(@RequestParam(defaultValue = "1") Integer currentPage,
                                   @RequestParam(defaultValue = "10") Integer size,
                                   Category category){
        return categoryService.getAllCategory(currentPage, size, category);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping("/addCategory")
    public RespBean<Integer> addCategory(@RequestBody CategoryVo category){
        return categoryService.addCategory(category);
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PostMapping("/editCategory")
    public RespBean<Integer> editCategory(@RequestBody Category category){
        return categoryService.editCategoryName(category);
    }

    /**
     * 根据分类id获取分类信息
     * @param category
     * @return
     */
    @PostMapping("/getCategoryById")
    public RespBean<Category> getCategoryById(@RequestBody Category category){
        return categoryService.selectCategoryById(category);
    }

    /**
     * 根据分类id删除分类
     * @param category
     * @return
     */
    @PostMapping("/deleteCategoryById")
    public RespBean<Integer> deleteCategoryById(@RequestBody Category category){
        return categoryService.deleteCategoryById(category);
    }
}
